package Third;

import java.io.*;

/**
 * description:实现文件的复制
 */
public class CopyFiles {

    public static void copyFiles(String oldPath,String newPath) {
        BufferedInputStream read = null;
        BufferedOutputStream write = null;

        try {
            //分别关联操作对象路径和目的路径
            read = new BufferedInputStream(new FileInputStream(oldPath));
            write = new BufferedOutputStream(new FileOutputStream(newPath));
            //初始化一个缓存数组
            byte[] arry = new byte[8*1024];
            int res = 0;
            //检测是否读取到文件末尾，如果没有写入数据
            while ((res = read.read(arry)) !=-1){
                write.write(arry,0,res);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(null != write){
                try {
                    write.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(null != read){
                try {
                    read.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
